/**
 * 
 */
package name.yzhu.httpserver.method;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import name.yzhu.common.util.Security;
import name.yzhu.common.yaml.Yaml;
import name.yzhu.httpserver.HttpReturnData;

import org.junit.Assert;

import com.google.common.io.Files;


/**
 * @author yhzhu
 *
 */
public class HttpHandleTestObject extends AbstractHttpHandler {
	final static String IMG1_NAME="img1.JPG";  
	public String getLoginQuery(){
		return "login&name=yhzhu&password=10";
	}
	
	@HttpMethodAnno(hrm="login", parameters="name,password")
	public byte[] login(String name, Integer password){
		System.out.println("name = "+name);
		System.out.println("password = "+password);
		
		String data = "name = "+name+" password = "+password+" utf8测试";
		
		byte[] b = data.getBytes();
		Assert.assertEquals("yhzhu", name);
		Assert.assertEquals(Integer.valueOf(10), password);
		return b;
	}
	
	public String getListQuery(){
		return "list&names=a,b,c,e,1";
	}
	
	@HttpMethodAnno(hrm="list", parameters="names")
	public byte[] testList(List<String> names){
		String str = Yaml.Dump(names);
		System.out.println(str);
		Assert.assertEquals("a", names.get(0));
		return str.getBytes();
	}
	
	public String getMapQuery(){
		return "map&name=yhzhu&users=z:26,g:2,c:3,d:4";
	}
	
	@HttpMethodAnno(hrm="map", parameters="name,users")
	public byte[] testMap(String name, Map<String, String> users){
		String str = Yaml.Dump(users);
		System.out.println(str);
		Assert.assertEquals("yhzhu", name);
		Assert.assertEquals("3", users.get("c"));
		return str.getBytes();
	}
	
	@HttpMethodAnno(hrm="get_image", parameters="name")
	public void getImage(String name){
		
	}
	
	@HttpMethodAnno(hrm="set_image", parameters="name,len")
	public void setImage(HttpServletRequest req, HttpServletResponse resp){
		
	}
	
	public String getNaiveQuery(){
		return "naive";
	}
	
	@HttpMethodAnno(hrm="naive", naive=true)
	public byte[] naive(HttpServletRequest req, HttpServletResponse resp){
		
		try {
			String name = req.getHeader("file-name");
			int len = req.getIntHeader("file-len");
			System.out.println("data len = "+len);
			byte[] data = new byte[8*1024];
			ServletInputStream is = req.getInputStream();
			int l = 0;
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			while((l = is.read(data)) != -1){
				bos.write(data, 0, l);
			}
			byte[] arr = bos.toByteArray();
			System.out.println("arr len = "+ arr.length);
			File file = new File(name);
			Files.write(arr, file);
			String md5 = Security.EncodeFile(file);
			HttpReturnData ret = new HttpReturnData();
			ret.addHead("file-name", name);
			ret.addHead("md5-code", md5);
			ret.addHead("file-len", String.valueOf(file.length()));
			return ret.toResult();
		} catch (IOException e) {
			e.printStackTrace();
			return HttpReturnData.toErrorResult(-5, e.getMessage());
		}
	}
}
